﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BattleCore;
using BattleCore.Events;
using PsyModules;

namespace Battle
{
    public class BattleBox
    {
        // Custom chat module
        private ShortChat msg = new ShortChat(); 

        private GameType _BoxGame = GameType.Normal;
        public GameType BoxGame
        {
            get { return _BoxGame; }
            set { _BoxGame = value; }
        }
        private List<string> _WaitQ = new List<string>();
        public List<string> WaitQ
        {
            get { return _WaitQ; }
            set { _WaitQ = value; }
        }
        private DateTime _GameDelay = DateTime.Now;
        public DateTime GameDelay
        {
            get { return _GameDelay; }
            set { _GameDelay = value; }
        }
        private PrizeState _Prizes;
        public PrizeState Prizes
        {
            get { return _Prizes; }
            set { _Prizes = value; }
        }
        private string _BoxName;
        public string BoxName
        {
            get { return _BoxName; }
            set { _BoxName = value; }
        }
        private string _BoxOwner;
        public string BoxOwner
        {
            get { return _BoxOwner; }
            set { _BoxOwner = value; }
        }
        private ushort[] _Location = new ushort[4];
        public ushort[] Location
        {
            get { return _Location; }
            set { _Location = value; }
        }
        private List<BattleTeam> _Teams = new List<BattleTeam>();
        public List<BattleTeam> Teams
        {
            get { return _Teams; }
            set { _Teams = value; }
        }
        private int _Lives = 0;
        public int Lives
        {
            get { return _Lives; }
            set { _Lives = value; }
        }
        private int _NumTeams = 0;
        public int NumTeams
        {
            get { return _NumTeams; }
            set { _NumTeams = value; }
        }
        private int _TeamSize = 0;
        public int TeamSize
        {
            get { return _TeamSize; }
            set { _TeamSize = value; }
        }
        private bool _GameOn = false;
        public bool GameOn
        {
            get { return _GameOn; }
            set { _GameOn = value; }
        }
        private bool _Locked = false;
        public bool Locked
        {
            get { return _Locked; }
            set { _Locked = value; }
        }
        public int? AvailableTeamOwner
        {
            get
            {
                for (int i = 0; i < _Teams.Count; i++)
                {
                    if (_Teams[i].TeamOwner == "~nobody")
                        return i;
                }
                return null;
            }
        }
        public void UpdateBox(Queue<EventArgs> q, List<int> BoxDelay)
        {
            if (!_Locked)
            {
                if (!_GameOn)
                {
                    if (_BoxGame == GameType.Duel &&
                        _Teams[0].TeamSlots[0].ActivePlayer.PlayerName != null &&
                        _Teams[1].TeamSlots[0].ActivePlayer.PlayerName != null)
                    {
                        //q.Enqueue(msg.arena("[ " + _BoxName + " ] - Start triggered. All teams detected full."));
                        InitiateGameStartDelay(q);
                        BoxDelay.Add(_Teams[0].TeamSlots[0].ActivePlayer.BoxNum);
                        _GameDelay = DateTime.Now;
                    }
                }
            }
        }
        private void InitiateGameStartDelay(Queue<EventArgs> q)
        {
            for (int i = 0; i < _Teams.Count; i++)
                for (int j = 0; j < _Teams[i].TeamSlots.Count; j++)
                {
                    _Teams[i].TeamSlots[j].ActivePlayer.SC_Stamp = DateTime.Now;
                    q.Enqueue(msg.pm(_Teams[i].TeamSlots[j].ActivePlayer.PlayerName,"You have 5 seconds to ship change before match begins."));
                }
        }
        public void StartGame()
        {
            _GameOn = true;
        }
        public void AddPlayerTo_Q(string PlayerName, Queue<EventArgs> q)
        {
        }
        public void SendBoxMessage(string message,Queue<EventArgs> q)
        {
            for (int j = 0; j < _Teams.Count; j++)
            {
                for (int k = 0; k < _Teams[j].TeamSlots.Count; k++)
                {
                    q.Enqueue(msg.pm(_Teams[j].TeamSlots[k].ActivePlayer.PlayerName, message));
                }
            }
        }
        public void GameOver(Queue<EventArgs> q)
        {
            _GameOn = false;
            _GameDelay = DateTime.Now;
            SendBoxMessage("*shipreset", q);
        }
    }
}
